package dpe;

import java.io.File;
import java.nio.ByteBuffer;

import org.lwjgl.Sys;
import org.lwjgl.opengl.Display;

public class Engine {
	public static final String FILE_EXTENSION = ".dpe";

	private Game game;

	public Game newGame() {
		if (game != null) {
			log(2, "Cant have multiple games");
			exit(1);
		}
		game = new Game();
		return game;
	}

	public void init() {
		game.init();
	}

	public World newWorld(String name) {
		World world = new World(game, name);
		game.addWorld(world);
		return world;
	}

	public static void log(int level, String msg) {
		String a = null;
		if (level == 0) {
			a = "[INFO]";
		} else if (level == 1) {
			a = "[WARNING]";
		} else if (level == 2) {
			a = "[ERROR]";
		}
		System.out.println("[DPE] " + a + ": " + msg);
	}

	public static void exit(int errorlevel) {
		if (Display.isCreated()) {
			Display.destroy();
		}
		System.exit(errorlevel);
	}

	public static void main(String[] args) {
		System.setProperty("org.lwjgl.librarypath", new File("lib/native").getAbsolutePath());
		Engine engine = new Engine();

		Display.setIcon(new ByteBuffer[] {});

		if (args.length == 0) {
			new EngineLoader(engine).loadGame("testgame.dpe");
		} else {
			log(1, "No arguments!");
			Engine.exit(1);
		}
	}
	
	public static long getTime() {
		return (Sys.getTime() * 1000) / Sys.getTimerResolution();
	}

	public void startGame() {
		game.start();
	}
}					